/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.activity;

import net.yougli.shakethemall.ColorPickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.log.Constants;

/**
 * Activity to change to settings. The activity is using the
 * res/xml/preferences.xml file to describe to user interface.
 * 
 * @author Michael Schmidt
 */
public class SettingsActivity extends PreferenceActivity {

	/**
	 * Adds preferences from res/xml/preferences.xml.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);

		Preference authBtn = findPreference(this.getResources().getString(
				R.string.settings_key_auth_btn));

		authBtn.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO OAuth with OSM server
				return true;
			}
		});

		Preference wayColor = findPreference(this.getResources().getString(
				R.string.settings_key_way_color));

		final Context context = this;

		wayColor.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				String key = getResources().getString(R.string.settings_key_way_color);
				String preferencesKey = getResources().getString(R.string.preferences_key_map);

				final SharedPreferences sharedPrefs = getSharedPreferences(preferencesKey,
						MODE_WORLD_WRITEABLE);
				int defaultWayColor = getResources().getColor(R.color.map_way_color);
				int currentColor = sharedPrefs.getInt(key, defaultWayColor);
				ColorPickerDialog.OnColorChangedListener listener = new ColorPickerDialog.OnColorChangedListener() {
					@Override
					public void colorChanged(String newKey, int color) {
						SharedPreferences.Editor editor = sharedPrefs.edit();
						editor.putInt(newKey, color);
						editor.commit();
						Log.d(Constants.SETTINGS, "new color: " + color);
					}
				};

				new ColorPickerDialog(context, listener, key, currentColor, currentColor)
						.show();

				return true;
			}
		});

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
	}

}
